package logic

import (
	"context"
	"errors"
	"fmt"
	"math/rand"
	"video-hubs/model/mysql"

	"video-hubs/videorpc/internal/svc"
	"video-hubs/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegineLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegineLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegineLogic {
	return &RegineLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegineLogic) Regine(in *videorpc.RegineRequest) (*videorpc.RegineResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	if err := user.FindUser(mysql.DB, in.Username); err == nil {
		return nil, errors.New("用户已存在")
	}
	data := &mysql.User{
		Username: in.Username,
		Password: in.Password,
		Image:    fmt.Sprintf("用户名_%d", rand.Intn(100000)),
		Title:    "https://p3.ssl.qhimgs1.com/t04fcdd69493b9e0a83.jpg",
	}
	if err := data.AddUsers(mysql.DB); err != nil {
		return nil, err
	}
	return &videorpc.RegineResponse{
		UserId: int64(data.ID),
	}, nil
}
